package com.ding.easycontroller.utils;

import com.ding.easycontroller.helper.EcMultiPartRequestImpl;
import com.ding.easycontroller.helper.EcRequest;
import com.ding.easycontroller.helper.EcRequestImpl;
import jdk.nashorn.api.scripting.ScriptObjectMirror;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.Enumeration;

//处理调用参数
@Component
public class ParamUtils {

    @Autowired
    private ScriptEngine scriptEngine;

    public Object[] params(HttpServletRequest httpServletRequest, HttpServletResponse response) {

        try {

            EcRequest request = new EcRequestImpl(httpServletRequest);

            //启动识别multipart请求并转换
            if (request.getContentType() != null && (request.getContentType().contains("multipart/form-data") || request.getContentType().contains("multipart/mixed")))
                request = new EcMultiPartRequestImpl(request);

            //session对象
            HttpSession session = request.getSession();

            //输入流
            ServletInputStream inputStream;
            try {
                inputStream = request.getInputStream();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

            //输入流转Reader
            Reader reader = new BufferedReader(new InputStreamReader(inputStream));

            //输出流
            ServletOutputStream outputStream;
            try {
                outputStream = response.getOutputStream();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

            //输出流转Writer
            Writer writer = new BufferedWriter(new OutputStreamWriter(outputStream));

            //创建一个Json对象
            ScriptObjectMirror reqParams = (ScriptObjectMirror) ((Invocable) scriptEngine).invokeFunction("__createJson");

            //获取所有请求参数
            Enumeration<String> parameterNames = request.getParameterNames();

            while (parameterNames.hasMoreElements()) {

                //获取请求参数的名称
                String name = parameterNames.nextElement();

                //获取所有请求参数
                String[] parameterValues = request.getParameterValues(name);

                //如果请求参数不是数组
                if (parameterValues.length == 1) {
                    reqParams.put(name, parameterValues[0]);
                } else {
                    ScriptObjectMirror array = (ScriptObjectMirror) ((Invocable) scriptEngine).invokeFunction("__createArray");
                    for (int i = 0; i < parameterValues.length; i++) {
                        array.setSlot(i, parameterValues[i]);
                    }
                    reqParams.put(name, array);
                }
            }


            //处理请求头
            ScriptObjectMirror reqHeads = (ScriptObjectMirror) ((Invocable) scriptEngine).invokeFunction("__createJson");
            Enumeration<String> headerNames = request.getHeaderNames();
            while (headerNames.hasMoreElements()) {
                String name = headerNames.nextElement();
                reqHeads.put(name, request.getHeader(name));
            }


            //参数有11个
            Object params[] = new Object[11];

            params[0] = request;
            params[1] = response;
            params[2] = reqHeads;
            params[3] = reader;
            params[4] = writer;
            params[5] = inputStream;
            params[6] = outputStream;
            params[7] = session;
            params[8] = reqParams;
            params[9] = null;
            params[10] = ((Invocable) scriptEngine).invokeFunction("__createJson");//响应头

            return params;

        } catch (Exception ignore) {
            return null;
        }

    }


}
